<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查看微博详情</title>
</head>
<body>
<div>
    <h1>{{weibo.nickname}}:{{weibo.content}}</h1>
    <p>发布于:{{weibo.created}}</p>
    <img v-for="url in weibo.urls.split(',')" :src="url" width="200">
    <hr>

    <h2>评论内容</h2>
    <input type="text" placeholder="评论内容..." v-model="comment.content">
    <input type="button" value="发评论" @click="insert()">

    <div v-for="c in arr">
        <h4>{{c.nickname}}评论说:{{c.content}}</h4>
        <p style="font-size: 12px">发布于:{{c.created}}</p>
    </div>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            weibo: {urls: ""},
            comment: {content: "", weiboId: ""},
            arr: []
        },
        methods: {
            insert() {
                v.comment.weiboId = v.weibo.id;
                //发出post异步请求
                axios.post("/comment/insert", v.comment).then(function (response) {
                    if (response.data == 1) {
                        alert("评论成功!")
                        location.reload();
                    } else {
                        alert("请先登录!");
                        location.href = "/login.html";
                    }
                })
            }
        },
        created: function () {
            /*location.search返回当前网页 URL 中的查询字符串部分。查询字符串是 URL 中跟随 "?" 字符的一部分，包含由 "&" 字符分隔的键值对
            即index页面中<a :href="'/detail.html?id='+weibo.id">的weibo.id
             */
            //请求当前微博详情
            axios.get("/weibo/selectById" + location.search).then(function (response) {
                v.weibo = response.data;
            })

            //请求当前微博对应的所有评论 需要传递微博id
            axios.get("/comment/select" + location.search).then(function (response) {
                v.arr = response.data;
            })
        }
    })
</script>

</body>
</html>